# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.common.action_chains import ActionChains
import unittest
import random
import time
from cookies import lasthopes, randomword
import re
import string
import sys
from selenium.webdriver.common.keys import Keys
from PIL import Image
from os import remove, listdir, sep
from os.path import basename, isfile
import tempfile
from warnings import warn
from basecart import BaseCart

class order_cancel_bad(BaseCart):
    def test_order_cancel_bad(self):
        driver = self.driver
        self.get_cat()
        itm_url = self.get_item()
        #Cart button in left upper corener
        cart_btn = driver.find_element_by_css_selector("div.b-header-top-cell-h > span.b-header-top-cell-anchor")
        self.buy_item()
        #Check for cart button renewed
        WebDriverWait(driver, timeout=10).until(lambda dr: not self.check_exists(cart_btn))
        #If ok go to cart
        self.go_cart()
        cred_block = self.perform_order()
        #Fill base essentials
        small_pdata = {'phone':str(random.randrange(9000000000, 9999999999)),\
                       'email':randomword(3, 9)+'@'+randomword(3, 6)+'.'+randomword(2, 3),\
                       'fullname':randomword(3, 9)+' '+randomword(3, 9)+' '+randomword(3, 9)}
        for key in small_pdata.keys():
            inpt = cred_block.find_element_by_name(key)
            inpt.clear()
            inpt.send_keys(small_pdata[key])
        newcity = self.change_city()
        self.get_delivery(u'Курьер')
        self.select_delivery_tooltip()
        self.fill_shipping_popup(newcity)
        self.catch_bad_popup()
        self.payment_choose()
        self.fill_loyalty()
        url_before = driver.current_url
        self.shot_an_element(driver.find_element_by_tag_name("body"), "full_page", False)
        self.confirm_order(url_before)
        self.shot_an_element(driver.find_element_by_tag_name("body"), "full_page", False)
        cancel_lnk = WebDriverWait(driver, timeout=10).until(lambda dr: dr\
                        .find_element_by_css_selector("span.g-pseudo.g-link.js-cart-order_complete-cancel"))
        cancel_lnk.click()
        cancel_pop = WebDriverWait(driver, timeout=10).until(lambda dr: dr\
                        .find_element_by_css_selector("div.b-popup.mod_ordercancel"))
        driver.execute_script("var flock = document.getElementsByName(\'flocktory\')[0];\
                               try{flock.parentNode.removeChild(flock);\
                                   delete flock;}\
                               catch(e){console.log(e.stack);}")
        try: WebDriverWait(driver, timeout=4).until(lambda dr: dr\
                                                    .find_element_by_tag_name("ins")).click()
        except: print "No <ins /> found. If flocktory still shown, try another method."
        #Check if only text
        reasonarea = WebDriverWait(cancel_pop, timeout=4).until(lambda dr: dr\
                            .find_element_by_css_selector("textarea.b-input-field"))
        reasonarea.send_keys(randomword(16, 64))
        cancel_pop.find_element_by_css_selector("input.b-sbutton-input").click() 
        self.find_error_tooltip(cancel_pop, u'Укажите причину отказа')
